package com.sy.service.message.impl;

import com.alibaba.excel.EasyExcel;
import com.sy.bean.message.Supplier;
import com.sy.bean.message.excel.GoodsExcel;
import com.sy.bean.message.excel.SupplierExcel;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.mapper.message.SupplierMapper;
import com.sy.service.message.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl extends BaseServiceImpl<Supplier, SupplierMapper> implements SupplierService {
    @Autowired
    SupplierMapper supplierMapper;

    @Autowired
    HttpServletRequest request;

    @Override
    public String exportSupplier(Map map) {
        List<SupplierExcel> list = supplierMapper.exportSupplier(map);
        String root = request.getServletContext().getRealPath("/") + "/excel";
        File file = new File(root);
        if (!file.exists())
            file.mkdirs();
        String newFileName = file.getAbsolutePath() + "/供应商列表.xlsx";
        EasyExcel.write(newFileName, SupplierExcel.class)
                .sheet("订单列表")
                .doWrite(() -> {
                    return list;
                });
        return "/excel/供应商列表.xlsx";
    }
}
